package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.come.entity.Goodstable;
import org.come.mouslisten.MyOptionalMouslisten;
import org.come.until.CutButtonImage;
import org.come.until.UserMessUntil;
import org.come.until.ZipUtils;

import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.RoleCaoZuoBtn;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
/**
 * 自选礼包
 * @author admin
 *
 */
public class MyOptionalJpanel extends JPanel {

	private List<RoleCaoZuoBtn> purchasebtnList = new ArrayList<>();
	public static List<JLabel> labGoodsImgsxList = new ArrayList<>();
	public static List<JLabel> goodsnameList = new ArrayList<>();
	private List<MyOptionalMouslisten> flipMouslistenList = new ArrayList<>();
	private List<MyOptionalMouslisten> flipGoodsMouslistenList = new ArrayList<>();
	private static FormsOnOffBtn offBtn; //
	public static String msg = "";
	public static JLabel labGoodsImgwx, labGoodsImgwsx, yidong, numbers;
	private static JTextField findName, findName1;
	public static Goodstable goods;
	public static Goodstable zhjgood = new Goodstable();
	public static int sxX;
	public static int sxY;
	public static int it = 4000;
	public static int dz = -1;
	public static boolean b = false;
	public static boolean start = false;
	public static List<String> godList;
	public static List<Goodstable> goodsList;

	public JLabel[] labGoodsImgsx = new JLabel[100];
	public static Sprite drawfp = SpriteFactory.VloadSprite("resource/loginButton/drawfp.tcp", null);

	public MyOptionalJpanel() {
		this.setPreferredSize(new Dimension(550, 680)); // 调整面板高度
		this.setLayout(null);
		this.setBackground(UIUtils.Color_BACK);
		showViewData();
	}

	/** 展示物品 */
	public void showViewData() {
		Font font = new Font("楷体", 1, 16);
		Font smallFont = new Font("楷体", 1, 14);
		goodsList = new ArrayList<>();

		// 清除现有组件
		for (RoleCaoZuoBtn btn : purchasebtnList) {
			this.remove(btn);
		}
		for (JLabel label : labGoodsImgsxList) {
			this.remove(label);
		}
		for (JLabel label : goodsnameList) {
			this.remove(label);
		}

		// 动态创建组件
		purchasebtnList.clear();
		labGoodsImgsxList.clear();
		goodsnameList.clear();
		flipMouslistenList.clear();
		flipGoodsMouslistenList.clear();
		for (int i = 0; i < labGoodsImgsx.length; i++) {
			if(labGoodsImgsx[i]!=null){
				labGoodsImgsx[i].setIcon(null);
			}
		}
		for (int i = 0; i < godList.size(); i++) {
			JLabel goodsname = new JLabel();
			RoleCaoZuoBtn purchasebtn = new RoleCaoZuoBtn("inkImg/button/领取.png", 1, UIUtils.COLOR_BLACK, font, "", 3011 + i);
			MyOptionalMouslisten flipMouslisten = new MyOptionalMouslisten(i, this, 1);
			int row = i / 5; // 计算当前元素所在的行
			int col = i % 5; // 计算当前元素所在的列
			purchasebtn.setBounds(90 * col + 60, 70 + 120 * row, 70, 60);
			goodsname.setBounds(95 * col + 40, 40 + 120 * row, 190, 90);
			purchasebtn.setVisible(true);
			this.add(purchasebtn);
			purchasebtnList.add(purchasebtn);
			goodsnameList.add(goodsname);

			labGoodsImgsx[i] = new JLabel();
			Goodstable goodstable = UserMessUntil.getgoodstable(new BigDecimal(godList.get(i)));
			goodsList.add(goodstable);
			goodsname.setFont(smallFont);
			goodsname.setForeground(UIUtils.COLOR_BLACK[0]);
			labGoodsImgsx[i].setIcon(CutButtonImage.getImage("img/item/" + goodstable.getSkin() + ".png", 70, 70));
			labGoodsImgsx[i].setBounds(90 * col + 60, 20 + 120 * row, 90, 90);
			MyOptionalMouslisten flipGoodsMouslisten = new MyOptionalMouslisten(i, this, 1);
			labGoodsImgsx[i].addMouseListener(flipGoodsMouslisten);
			goodsname.setText(goodstable.getGoodsname());
			labGoodsImgsx[i].setVisible(true);
			this.add(labGoodsImgsx[i]);
			labGoodsImgsxList.add(labGoodsImgsx[i]);
			flipMouslistenList.add(flipMouslisten);
			flipGoodsMouslistenList.add(flipGoodsMouslisten);
		}
	}

	private ImageIcon icon2 = new ImageIcon("inkImg/background/自选道具.png");

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(icon2.getImage(), 0, 0, 550, 680, this); // 调整背景图高度
		// 绘制网格线
		g.setColor(Color.LIGHT_GRAY); // 设置网格线颜色
		int cellWidth = 90;
		int cellHeight = 120;
		int startX = 60;
		int startY = 20;

		// 绘制水平网格线
		for (int i = 0; i <= (godList.size() - 1) / 5; i++) {
			int y = startY + i * cellHeight;
			g.drawLine(startX, y, startX + 5 * cellWidth, y);
		}
	}

	public static JTextField getFindName() {
		return findName;
	}

	public static void setFindName(JTextField findName) {
		MyOptionalJpanel.findName = findName;
	}
}

